package com.unitd.base.module.consumer.controller;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.unitd.base.frame.plugin.module.dto.ResponseUtils;
import com.unitd.base.frame.util.ListUtils;
import com.unitd.base.module.api.entity.BrandVo;
import com.unitd.base.module.api.entity.CartVo;
import com.unitd.base.module.api.entity.GoodsVo;
import com.unitd.base.module.api.entity.TopicVo;
import com.unitd.base.module.api.service.ApiBrandService;
import com.unitd.base.module.api.service.ApiCartService;
import com.unitd.base.module.api.service.ApiGoodsService;
import com.unitd.base.module.api.service.ApiTopicService;
import com.unitd.base.module.consumer.comm.annotation.IgnoreAuth;
import com.unitd.base.module.consumer.comm.controller.ApiBaseAction;
import com.unitd.base.module.master.pojo.dto.AdvertisingVO;
import com.unitd.base.module.master.pojo.dto.CategoryVO;
import com.unitd.base.module.master.pojo.dto.ChannelVO;
import com.unitd.base.module.master.service.AdvertisingService;
import com.unitd.base.module.master.service.CategoryService;
import com.unitd.base.module.master.service.ChannelService;
import com.unitd.base.module.shoe.pojo.dto.GoodsVO;
import com.unitd.base.module.shoe.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

/**
 * 作者: @author Harmon <br>
 * 时间: 2017-08-11 08:32<br>
 *
 * @gitee https://gitee.com/fuyang_lipengjun/platform
 * 描述: ApiIndexController <br>
 */
@Api(tags = "首页接口文档")
@RestController
@RequestMapping("/api/index")
public class ApiIndexController extends ApiBaseAction {

    @Autowired
    private AdvertisingService advertisingService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ApiGoodsService goodsService;
    @Autowired
    private ApiBrandService brandService;
    @Autowired
    private ApiTopicService topicService;
    @Autowired
    private ApiCartService cartService;

    @Autowired
    private GoodsService goodsServiceImpl;

    /**
     * 测试
     */
    @IgnoreAuth
    @PostMapping(value = "test")
    public Object test() {
        return ResponseUtils.toResponsMsgSuccess("请求成功yyy");
    }

    /**
     * app首页
     */
    @ApiOperation(value = "首页")
    @IgnoreAuth
    @PostMapping(value = "index")
    public Object index() {
        Map<String, Object> resultObj = Maps.newHashMap();
        List<AdvertisingVO> banner = advertisingService.selectByPositionId(1L);
        resultObj.put("banner", banner);

        List<ChannelVO> channel = channelService.selectBySort();
        resultObj.put("channel", channel);

        Map param = Maps.newHashMap();
        param.put("is_new", 1);
        param.put("is_delete", 0);
//        param.put("fields", "id, name, list_pic_url, retail_price");
        PageHelper.startPage(0, 4, false);
        List<GoodsVo> newGoods = goodsService.queryList(param);
        resultObj.put("newGoodsList", newGoods);
        //
        param = Maps.newHashMap();
        param.put("is_hot", "1");
        param.put("is_delete", 0);
        PageHelper.startPage(0, 3, false);
        List<GoodsVo> hotGoods = goodsService.queryHotGoodsList(param);
        resultObj.put("hotGoodsList", hotGoods);
        // 当前购物车中
        List<CartVo> cartList = new ArrayList<>();
        if (null != getUserId()) {
            //查询列表数据
            Map<String, Object> cartParam = Maps.newHashMap();
            cartParam.put("user_id", getUserId());
            cartList = cartService.queryList(cartParam);
        }

        if (!ListUtils.isNull(cartList) && !ListUtils.isNull(hotGoods)) {
            for (GoodsVo goodsVo : hotGoods) {
                for (CartVo cartVo : cartList) {
                    if (goodsVo.getId().equals(cartVo.getGoods_id())) {
                        goodsVo.setCart_num(cartVo.getNumber());
                    }
                }
            }
        }
        //
        param = Maps.newHashMap();
        param.put("is_new", 1);
        param.put("sidx", "new_sort_order ");
        param.put("order", "asc ");
        param.put("offset", 0);
        param.put("limit", 4);
        List<BrandVo> brandList = brandService.queryList(param);
        resultObj.put("brandList", brandList);

        param = Maps.newHashMap();
        param.put("offset", 0);
        param.put("limit", 3);
        List<TopicVo> topicList = topicService.queryList(param);
        resultObj.put("topicList", topicList);

        param = Maps.newHashMap();
        param.put("parent_id", 0);
        param.put("notName", "推荐");
        List<CategoryVO> categoryList = categoryService.selectByMap(param);

        List<Map<String, Object>> newCategoryList = new ArrayList<>();
        for (CategoryVO categoryItem : categoryList) {
//            param.remove("fields");
            param.put("parent_id", categoryItem.getId());
            List<CategoryVO> categoryEntityList = categoryService.selectByMap(param);
            List<Long> childCategoryIds = new ArrayList<>();
            for (CategoryVO categoryEntity : categoryEntityList) {
                childCategoryIds.add(categoryEntity.getId());
            }
            //
            param = Maps.newHashMap();
            param.put("categoryIds", childCategoryIds);
//            param.put("fields", "id as id, name as name, list_pic_url as list_pic_url, retail_price as retail_price");
            PageHelper.startPage(0, 7, false);
            List<GoodsVo> categoryGoods = goodsService.queryList(param);
            Map<String, Object> newCategory = Maps.newHashMap();
            newCategory.put("id", categoryItem.getId());
            newCategory.put("name", categoryItem.getName());
            newCategory.put("goodsList", categoryGoods);
            newCategoryList.add(newCategory);
        }
        resultObj.put("categoryList", newCategoryList);
        return ResponseUtils.toResponsSuccess(resultObj);
    }

    /**
     * app首页
     */
    @ApiOperation(value = "新商品信息")
    @PostMapping(value = "newGoods")
    public Object newGoods() {
        Map<String, Object> resultObj = Maps.newHashMap();

        Map<String, Object> param = Maps.newHashMap();
        param.put("F8500","1");//上架商品
        List<GoodsVO> newGoods = goodsServiceImpl.selectByBean(getUserDefDbName(),param);

        if(newGoods==null){
            newGoods = Lists.newArrayList();
        }

        Map<String, List<GoodsVO>> collect = newGoods.stream().collect(Collectors.groupingBy(GoodsVO::getF3127_itemNo));

        List<GoodsVO> respNewGoods = Lists.newArrayList();

        if(collect!=null&&!collect.isEmpty()){
            collect.forEach((k,v)->{
                GoodsVO goodsVO = v.get(0);
                respNewGoods.add(goodsVO);
            });
        }

        resultObj.put("newGoodsList", respNewGoods);

        return ResponseUtils.toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "新热门商品信息")
    @IgnoreAuth
    @PostMapping(value = "hotGoods")
    public Object hotGoods() {
        Map<String, Object> resultObj = Maps.newHashMap();

        Map<String, Object> param = Maps.newHashMap();
        param.put("is_hot", "1");
        param.put("is_delete", 0);
        PageHelper.startPage(0, 3, false);
        List<GoodsVo> hotGoods = goodsService.queryHotGoodsList(param);
        resultObj.put("hotGoodsList", hotGoods);

        return ResponseUtils.toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "topic")
    @IgnoreAuth
    @PostMapping(value = "topic")
    public Object topic() {
        Map<String, Object> resultObj = Maps.newHashMap();

        Map<String, Object> param = Maps.newHashMap();
//        param.put("offset", 0);
//        param.put("limit", 3);
        List<TopicVo> topicList = topicService.queryList(param);
        resultObj.put("topicList", topicList);

        return ResponseUtils.toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "brand")
    @IgnoreAuth
    @PostMapping(value = "brand")
    public Object brand() {
        Map<String, Object> resultObj = Maps.newHashMap();
        Map<String, Object> param = Maps.newHashMap();
        param.put("is_new", 1);
//        param.put("sidx", "new_sort_order ");
//        param.put("order", "asc ");
//        param.put("offset", 0);s
//        param.put("limit", 4);
        List<BrandVo> brandList = brandService.queryList(param);
        resultObj.put("brandList", brandList);

        return ResponseUtils.toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "category")
    @IgnoreAuth
    @PostMapping(value = "category")
    public Object category() {
        Map<String, Object> resultObj = Maps.newHashMap();

        Map<String, Object> param = Maps.newHashMap();
        param.put("parent_id", 0);
//        param.put("notName", "推荐");
        List<CategoryVO> categoryList = categoryService.selectByMap(param);
        List<Map<String, Object>> newCategoryList = Lists.newArrayList();

        for (CategoryVO categoryItem : categoryList) {
//            param.remove("fields");
//            param.put("parent_id", categoryItem.getId());
            List<CategoryVO> categoryEntityList = categoryService.selectByMap(param);
            List<Long> childCategoryIds = null;
            if (!ListUtils.isNull(categoryList)) {
                childCategoryIds = new ArrayList<>();
                for (CategoryVO categoryEntity : categoryEntityList) {
                    childCategoryIds.add(categoryEntity.getId());
                }
            }

            param = Maps.newHashMap();
//            param.put("categoryIds", childCategoryIds);
//            param.put("fields", "id as id, name as name, list_pic_url as list_pic_url, retail_price as retail_price");
//            param.put("is_delete", "0");
            PageHelper.startPage(0, 7, false);
            List<GoodsVo> categoryGoods = goodsService.selectByCondtion(childCategoryIds, "0");
            Map<String, Object> newCategory = Maps.newHashMap();
            newCategory.put("id", categoryItem.getId());
            newCategory.put("name", categoryItem.getName());
            newCategory.put("goodsList", categoryGoods);
            newCategoryList.add(newCategory);
        }
        resultObj.put("categoryList", newCategoryList);

        return ResponseUtils.toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "banner")
    @IgnoreAuth
    @PostMapping(value = "banner")
    public Object banner() {
        Map<String, Object> resultObj = Maps.newHashMap();
        List<AdvertisingVO> banner = advertisingService.selectByPositionId(1L);
        resultObj.put("banner", banner);

        return ResponseUtils.toResponsSuccess(resultObj);
    }

    @ApiOperation(value = "channel")
    @IgnoreAuth
    @PostMapping(value = "channel")
    public Object channel() {
        Map<String, Object> resultObj = Maps.newHashMap();
        List<ChannelVO> channel = channelService.selectBySort();
        resultObj.put("channel", channel);

        return ResponseUtils.toResponsSuccess(resultObj);
    }
}